

package cn.shengchao.examstar.product.ohs.remote.rpc;

import cn.hutool.core.util.StrUtil;
import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.comment.acl.ports.repositories.message.CourseListDto;
import cn.shengchao.examstar.product.acl.ports.repositories.IProductRepository;
import cn.shengchao.examstar.product.ohs.local.appservice.CourseService;
import cn.shengchao.examstar.product.ohs.local.message.CourseInfoDto;
import cn.shengchao.examstar.rpc.product.api.ICourseRpc;
import cn.shengchao.examstar.rpc.product.req.CourseQueryPage;
import cn.shengchao.examstar.rpc.product.req.SaveCourseInfo;
import cn.shengchao.examstar.rpc.product.res.CourseList;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import top.continew.starter.cache.redisson.util.RedisUtils;
import top.continew.starter.core.validation.CheckUtils;

import java.util.List;

import static cn.shengchao.base.constant.Constants.RedisKey.PRODUCT_KEY;

@DubboService
public class CourseRpc implements ICourseRpc {

    @Autowired
    private IProductRepository iProductRepository;

    @Autowired
    private CourseService courseService;

    @Override
    public PageResult<CourseList> getCourseList(CourseQueryPage courseQueryPage) {
        if (StrUtil.isNotBlank(courseQueryPage.getSearchValue())) {
            List<Long> courseIds = iProductRepository.findCourseIdByTeacherName(courseQueryPage.getSearchValue());
            courseQueryPage.setCourseIds(courseIds);
        }
        Page<CourseListDto> page = PageHelper.startPage(courseQueryPage.getPage(), courseQueryPage.getSize());
        List<CourseListDto> record = iProductRepository.getCourseList(courseQueryPage);
        PageResult<CourseList> pageResult = PageResult.of(page.getTotal(), record.stream().map(item -> {
            CourseList courseList = new CourseList();
            courseList.setId(item.getId());
            courseList.setProductName(item.getProductName());
            courseList.setSpecificationType(item.getSpecificationType());
            courseList.setTeacher(item.getTeacher());
            courseList.setPrice(item.getPrice());
            courseList.setImageUrl(item.getImageUrl());
            courseList.setCourseType(item.getCourseType());
            courseList.setDiscountPrice(item.getDiscountPrice());
            courseList.setStatus(item.getStatus());
            courseList.setSort(item.getSort());
            courseList.setKnowledgeNodeNum(item.getKnowledgeNodeNum());
            return courseList;
        }).toList());
//        Map<String, BigDecimal> bigDecimalMap = iProductRepository.sumCourseListPrice(courseQueryPage);
//        JSONObject jsonObject = new JSONObject(bigDecimalMap);
//        pageResult.setExtensions(jsonObject);
        return pageResult;
    }
    @Transactional
    @Override
    public void editCourse(SaveCourseInfo saveCourseInfo) {
        RedisUtils.delete(PRODUCT_KEY + saveCourseInfo.getId());
        courseService.editCourse(saveCourseInfo);
    }

    @Override
    public SaveCourseInfo getCourseInfo(Long courseId) {
        CourseInfoDto courseInfo = courseService.getCourseInfo(courseId);
        CheckUtils.throwIfNull(courseInfo, "课程不存在！");
        SaveCourseInfo saveCourseInfo = new SaveCourseInfo();
        saveCourseInfo.setId(courseInfo.getId());
        saveCourseInfo.setProductName(courseInfo.getName());
        saveCourseInfo.setImageUrl(courseInfo.getImgUrl());
        saveCourseInfo.setPrice(courseInfo.getPrice());
        saveCourseInfo.setCardUrl(courseInfo.getCardUrl());
        saveCourseInfo.setDiscountPrice(courseInfo.getDiscountPrice());
        saveCourseInfo.setDetails(courseInfo.getDetails());
        saveCourseInfo.setSpecificationType(courseInfo.getSpecificationType());
        saveCourseInfo.setSort(courseInfo.getSort());
        saveCourseInfo.setStatus(courseInfo.getStatus());
        saveCourseInfo.setClassHour(courseInfo.getClassHour());
        return saveCourseInfo;
    }

    @Override
    public void deleteCourse(Long courseId) {
        RedisUtils.delete(PRODUCT_KEY + courseId);
        courseService.deleteCourse(courseId);
    }

    @Override
    public void changeStatus(Long courseId) {
        RedisUtils.delete(PRODUCT_KEY + courseId);
        courseService.changeStatus(courseId);
    }
}